package com.example.distributelock.service;

import com.example.distributelock.lock.RedisLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @describte:使用Redis分布式锁避免定时任务重复执行
 * @program:distribute-lock
 * @author:zhengdp
 * @create:2021-09-22 16:02:30
 **/
@Service
@Slf4j
public class SchedulerService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 使用分布式锁来解决定时任务集群重复执行的问题
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void sendSms(){
        try(RedisLock lock = new RedisLock(redisTemplate,"autoSms",30)){
            if(lock.getLock()){
                log.info("向13154894531发送短信！！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
